#testhotkey.py
import pythoncom, pyHook 

HOTLIST = []
LHISTORY = 5
KEYDOWN = []


def isHotekeyPressed(hotkey):
    """Check if hotkey is pressed in any order
        Hotkey is given as KeyID"""
    ret = True
    hotkey.reverse()

    try:
        if len(hotkey)<= len(HOTLIST):
            HotlistKeyID = map(lambda x: x.KeyID,HOTLIST[:len(hotkey)])
            for hotk in hotkey:
                if not(hotk in HotlistKeyID):
                    ret = False
        else:
            ret = False
    except:
        ret = False

    return ret
        

def isHotekeyPressedOrder(hotkey):
    """Check if hotkey is pressed in order 
        Hotkey is given as KeyID"""
    ret = False
    hotkey.reverse()

    try: 
        if map(lambda x: x.KeyID,HOTLIST[:len(hotkey)]) == hotkey:
            ret = True
    except:
        pass

    return ret
        

    
 
def OnKeyDown(event):
    print 'MessageName:',event.MessageName
##    print 'Message:',event.Message
##    print 'Time:',event.Time
##    print 'Window:',event.Window
##    print 'WindowName:',event.WindowName
##    print 'Ascii:', event.Ascii, chr(event.Ascii)
##    print 'Key:', event.Key
##    print 'KeyID:', event.KeyID
##    print 'ScanCode:', event.ScanCode
##    print 'Extended:', event.Extended
##    print 'Injected:', event.Injected
##    print 'Alt', event.Alt
##    print 'Transition', event.Transition
##    


    #Creat FIFO history list insert on left exit right
    HOTLIST.insert(0,event)
    if len(HOTLIST) > LHISTORY:
        HOTLIST.pop()
    print "History KeyID:", map(lambda x: x.KeyID,HOTLIST)   


    #test hotkey alt-f3, ALT = 164, F3 = 114, F2 113, 
    print "ALT F3", str(isHotekeyPressed([164,114]))

    #test hotkey ctrl = 162 shift = 160 3 = 51
    print "ctrl shift 3", str(isHotekeyPressed([162,160,51]))
    
    
    print '---'
    # return True to pass the event to other handlers
    return True
 
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyDown

# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
